package com.watson.utils;

import static android.content.Context.NOTIFICATION_SERVICE;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;

import com.watson.R;
import com.watson.activity.Details2Activity;

import java.util.Random;

public class NotificationHelper {

    public static void sendNotification(Context mContext, String title, String message, String no) {
        NotificationManager manager = (NotificationManager) mContext.getSystemService(NOTIFICATION_SERVICE);
        LogUtils.e("RabbitMQ", "ni---->" + no.replace("-", ""));
        // 创建一个点击通知后要执行的意图
        Intent intent = new Intent(mContext, Details2Activity.class);
        intent.putExtra("no", no.replace("-", ""));
        intent.putExtra("zt", "");
        intent.putExtra("type", 2);
        // 创建Random对象
        Random random = new Random();
        // 生成一个随机整数
        int randomInt = random.nextInt();
        LogUtils.e("RabbitMQ", "randomInt---->" + randomInt);
        PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_IMMUTABLE);

        if (Build.VERSION.SDK_INT >= 26) {
            LogUtils.e("TAG", "----------------------------");
            // API 26 以上的方法
            NotificationChannel channel = new NotificationChannel("channel_id", "Channel Name", NotificationManager.IMPORTANCE_HIGH);
            manager.createNotificationChannel(channel);
            Notification notification = new Notification.Builder(mContext, "channel_id")
                    .setContentTitle(title)
                    .setContentText(message)
                    .setContentIntent(pendingIntent)
                    .setSmallIcon(R.mipmap.icon)
                    .setAutoCancel(true) // 设置点击通知后自动清除通知
                    .build();
            manager.notify(1, notification);
        } else {
            LogUtils.e("TAG", "=============================");
            // API 26 以下的方法
            Notification notification = new Notification.Builder(mContext)
                    .setContentTitle(title)
                    .setContentText(message)
                    .setSmallIcon(R.mipmap.icon)
                    .setContentIntent(pendingIntent)
                    .setAutoCancel(true) // 设置点击通知后自动清除通知
                    .build();
            manager.notify(1, notification);
        }
    }

    public static void sendNotification2(Context mContext, String title, String message, String no) {
        NotificationManager manager = (NotificationManager) mContext.getSystemService(NOTIFICATION_SERVICE);
        LogUtils.e("RabbitMQ", "ni---->" + no.replace("-", ""));

        if (Build.VERSION.SDK_INT >= 26) {
            LogUtils.e("TAG", "----------------------------");
            // API 26 以上的方法
            NotificationChannel channel = new NotificationChannel("channel_id", "Channel Name", NotificationManager.IMPORTANCE_HIGH);
            manager.createNotificationChannel(channel);
            Notification notification = new Notification.Builder(mContext, "channel_id")
                    .setContentTitle(title)
                    .setContentText(message)
                    .setSmallIcon(R.mipmap.icon)
                    .setAutoCancel(true) // 设置点击通知后自动清除通知
                    .build();
            manager.notify(1, notification);
        } else {
            LogUtils.e("TAG", "=============================");
            // API 26 以下的方法
            Notification notification = new Notification.Builder(mContext)
                    .setContentTitle(title)
                    .setContentText(message)
                    .setSmallIcon(R.mipmap.icon)
                    .setAutoCancel(true) // 设置点击通知后自动清除通知
                    .build();
            manager.notify(1, notification);
        }
    }
}
